package com.xmg.p2p.business.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.business.domain.*;
import com.xmg.p2p.business.mapper.AccountFlowMapper;
import com.xmg.p2p.business.service.IAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * lcd  2020/5/27
 * Description:
 */
@Service
public class AccountFlowServiceImpl implements IAccountFlowService {
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    public void offLineCharge(RechargeOffline r, Account applier) {
        AccountFlow accountFlow = getBaseAccountFlow(applier);
        accountFlow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
        accountFlow.setAmount(r.getAmount());
        accountFlow.setNote("线下充值成功,充值金额:"+r.getAmount());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void bid(Account currentAccount, Bid bid) {
        AccountFlow accountFlow = getBaseAccountFlow(currentAccount);
        accountFlow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_BID_FREEZED);
        accountFlow.setAmount(bid.getAvailableAmount());
        accountFlow.setNote("投标成功"+bid.getBidRequestTitle()+",冻结账户下的投标金额:"+bid.getAvailableAmount());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void returnMoney(Account account, Bid bid) {
        AccountFlow accountFlow = getBaseAccountFlow(account);
        accountFlow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
        accountFlow.setAmount(bid.getAvailableAmount());
        accountFlow.setNote("投标审核失败"+bid.getBidRequestTitle()+",冻结账户下的投标金额返还:"+bid.getAvailableAmount());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void borrowSuccess(Account account, BidRequest bidRequest) {
        AccountFlow accountFlow = getBaseAccountFlow(account);
        accountFlow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL);
        accountFlow.setAmount(bidRequest.getBidRequestAmount());
        accountFlow.setNote("借款通过审核"+bidRequest.getTitle()+",借款人账户增加金额:"+bidRequest.getBidRequestAmount());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void chargeBorrowFee(BigDecimal manageChargeFee, BidRequest br, Account account) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_CHARGE);
        flow.setAmount(manageChargeFee);
        flow.setNote("借款" + br.getTitle() + "成功,支付借款手续费:" + manageChargeFee);
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void bidSuccess(Bid bid, Account account) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
        flow.setAmount(bid.getAvailableAmount());
        flow.setNote("投标" + bid.getBidRequestTitle() + "成功,取消投标冻结金额:"
                + bid.getAvailableAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void moneyWithDrawApply(MoneyWithdraw m, Account account) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_FREEZED);
        flow.setAmount(m.getAmount());
        flow.setNote("提现申请 金额" + m.getAmount());
        this.accountFlowMapper.insert(flow);
    }

    @Override
    public void withdrawFail(Account account, MoneyWithdraw m) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_UNFREEZED);
        flow.setAmount(m.getAmount());
        flow.setNote("提现失败 冻结金额转为可用金额" + m.getAmount());
        accountFlowMapper.insert(flow);
    }

    @Override
    public void withdrawSuccessReal(Account account, BigDecimal money) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW);
        flow.setAmount(money);
        flow.setNote("提现成功,账户真正增加金额" + money);
        accountFlowMapper.insert(flow);
    }

    @Override
    public void withdrawSuccessFee(Account account, BigDecimal money) {
        AccountFlow flow = getBaseAccountFlow(account);
        flow.setAccountType(BidConst.ACCOUNT_ACTIONTYPE_WITHDRAW_MANAGE_CHARGE);
        flow.setAmount(money);
        flow.setNote("提现成功,账户扣除手续费" + money);
        accountFlowMapper.insert(flow);
    }

    private AccountFlow getBaseAccountFlow(Account account) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setAccountId(account.getId());
        accountFlow.setUsableAmount(account.getUsableAmount());
        accountFlow.setFreezedAmount(account.getFreezedAmount());
        accountFlow.setTradeTime(new Date());
        return accountFlow;
    }

}
